/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Test of basic types (base module).
 *//*
 * LEGAL:   COPYRIGHT (C) 2005 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define TESTING 1
#include <vector>
#include "test.hh"
#include "base/module.hh"
#include "base/bits.hh"
#include "base/iterator.hh"
using namespace base;
#include "math/module.hh"
#include "math/funcs.hh"
using namespace math;

/*****************************************************************************
 * Test Bits class.
 *****************************************************************************/
bool
TestBits( void )
{
    CDEBUG << "============================================\n";
    CDEBUG << "Testing Bits class. \n";
    CDEBUG << "============================================\n";
 {
    Bits bits;
    if ( bits != 0 ) return Fail();  // should be zeros

    bits.Set( 0 );
    if ( bits != uint(1<<0) ) return Fail();
    if ( not bits.Get( 0 ) ) return Fail();

    bits.Set( 1 );
    if ( bits != uint((1<<0)|(1<<1)) ) return Fail();
    if ( not bits.Get( 1 ) ) return Fail();

    bits.Set( 31 );
    if ( bits != uint((1<<0)|(1<<1)|(1<<31)) ) return Fail();
    if ( not bits.Get( 31 ) ) return Fail();

    bits.Clear( 0 );
    if ( bits != uint((1<<1)|(1<<31)) ) return Fail();
    if ( bits.Get( 0 ) ) return Fail();

    bits = uint((1<<16)|(1<<24));
    if ( bits != uint((1<<16)|(1<<24)) ) return Fail();
    if ( not bits.Get( 16 ) ) return Fail();
    if ( not bits.Get( 24 ) ) return Fail();
    if ( bits.Get( 0 ) ) return Fail();
    if ( bits.Get( 1 ) ) return Fail();

    bits = 0;
    bits.Set( 4, true );
    if ( not bits.Get(4) || uint(bits) != 16 ) return Fail();
    bits.Set( 4, false );
    if ( bits.Get(4) || uint(bits) != 0 ) return Fail();
 }
    CDEBUG << "ok" << std::endl;
    return true;
}

/*****************************************************************************
 * Test Iterator class.
 *****************************************************************************/
bool
TestIterator( void )
{
    CDEBUG << "============================================\n";
    CDEBUG << "Testing Iterator class. \n";
    CDEBUG << "============================================\n";
 {
    //....................
  {
    // vector is empty.
    std::vector<int> vec;
    int loops = 0;
    STLIterator<int,std::vector<int> > iter( vec );
    while ( iter )
    {
        Fail();  // should not be reached
        ++loops;
    }
    if ( loops != 0 ) Fail();
  }
    //....................


    //....................
    // vec has 1 item.
  {
    std::vector<int> vec;
    vec.push_back( 123 );
    int loops = 0;
    STLIterator<int,std::vector<int> > iter( vec );
    while ( iter )
    {
        if ( loops == 0 )
        {
            int val = iter.Next();
            if ( val != 123 ) Fail();
        }
        else { Fail(); }

        ++loops;
    }
    if ( loops != 1 ) Fail();
  }


    //....................
    // vec has 100 items.
  {
    std::vector<int> vec;
    for ( uint i = 0; i < 100; ++i ) vec.push_back( i );
    int loops = 0;
    STLIterator<int,std::vector<int> > iter( vec );
    while ( iter )
    {
        int val = iter.Next();
        if ( val != loops ) Fail();
        ++loops;
    }
    if ( loops != 100 ) Fail();
  }

 }

    CDEBUG << "ok \n";
    return true;
}

/*****************************************************************************
 * main.
 *****************************************************************************/
int main( int argc, char** argv )
{
    RUN_TEST( "basic types", TEST_ERROR_MODE_ABORT,
              TestBits()
          and TestIterator() )
}
